#include <stdio.h>
#include "Stack.h"
#include <stdbool.h>

int Judge(char character)
{
	int flag = 0;
	if (character == '+')
		flag = 1;
	else if (character == '-')
		flag = 2;
	else if (character == '*')
		flag = 3;
	else if (character == '/')
		flag = 4;

	return flag;
}

int Input(OPTR * Operate, OPND * Arithmetic,int * arr)
{
	char character;
	int num = 0;
	char priority;
	int num1 = 0;
	int num2 = 0;
	int result = 0;
	int i = 0;
	char temp = 0;
	Stack_Init_OPTR(Operate);
	Stack_Init_OPND(Arithmetic);
	Operate->element[0] = '#';
	Operate->top = 0;
	while (scanf_s("%c",&character))
	{
		if (temp == '=' && character == '=')
		{
			break;
		}
		if (character == '\n')
		{
			continue;
		}
		if (character != '=')
		{
			if (character >= '0' && character <= '9')
			{
				//num = character - '0';
				
					num = num * 10 + (character - '0');
				
			}
			else if (character == '+' || character == '-' || character == '*' || character == '/' || character == '(' || character == ')')
			{
				//Stack_Push_OPTR(Arithmetic, num);
				if (character != '(') //操作数入栈
				{
					if (Operate->element[Operate->top] != ')')
					{
						Stack_Push_OPND(Arithmetic, num);
						num = 0;
					}
				}
				if (Operate->element[Operate->top] == '#')
				{
					Stack_Push_OPTR(Operate,character);
				}
				else //不是第一个进去的操作符
				{
					priority = getPriority(Operate->element[Operate->top], character);
					if(priority == '>')  //当栈顶的元素大于写入的操作符优先级
					{
						//处理有括号的情况
						if (Operate->element[Operate->top] == '(') //前一个元素是前括号就把后一个元素入栈
						{
							Stack_Push_OPTR(Operate, character);
						}
						else if (Operate->element[Operate->top] == ')')//前一个元素是后括号就处理括号内的所有运算
						{
							Stack_Pop_OPTR(Operate);//弹出此时的栈顶 )
							while (Operate->element[Operate->top] != '(')
							{
								num1 = Stack_Pop_OPND(Arithmetic);
								num2 = Stack_Pop_OPND(Arithmetic);
								if (Judge(Operate->element[Operate->top]) == 1)
									result = num2 + num1;
								else if (Judge(Operate->element[Operate->top]) == 2)
									result = num2 - num1;
								else if (Judge(Operate->element[Operate->top]) == 3)
									result = num2 * num1;
								else if (Judge(Operate->element[Operate->top]) == 4)
									result = num2 / num1;
								Stack_Pop_OPTR(Operate);
								Stack_Push_OPND(Arithmetic,result);
							}
							Stack_Pop_OPTR(Operate);//弹出此时的栈顶 (
						}
						//以上

						//处理无括号的情况
						if (Judge(Operate->element[Operate->top]) == 1)
						{
							num1 = Stack_Pop_OPND(Arithmetic);
							num2 = Stack_Pop_OPND(Arithmetic);
							result = num2 + num1;
						}
						else if (Judge(Operate->element[Operate->top]) == 2)
						{
							num1 = Stack_Pop_OPND(Arithmetic);
							num2 = Stack_Pop_OPND(Arithmetic);
							result = num2 - num1;
						}
						else if (Judge(Operate->element[Operate->top]) == 3)
						{
							num1 = Stack_Pop_OPND(Arithmetic);
							num2 = Stack_Pop_OPND(Arithmetic);
							result = num2 * num1;
						}
						else if (Judge(Operate->element[Operate->top]) == 4)
						{
							num1 = Stack_Pop_OPND(Arithmetic);
							num2 = Stack_Pop_OPND(Arithmetic);
							result = num2 / num1;
						}
						Stack_Push_OPND(Arithmetic, result);
						Stack_Pop_OPTR(Operate);
						Stack_Push_OPTR(Operate,character);
					}
					else if (priority == '<') //当栈顶元素的优先级小于写入元素的优先级
					{
						Stack_Push_OPTR(Operate, character); //push该操作符
					}
					else if (priority == '=')//当写入元素和栈顶元素相等的时候，说明写的是（），直接pop掉
					{
						Stack_Pop_OPTR(Operate);
						Stack_Pop_OPTR(Operate);
					}
					else if (priority == '0')
					{
						printf("Input ERROR!!!");
						break;
					}
				}
			}
			
		}
		else
		{
			if (Operate->element[Operate->top] != ')')
			{
				Stack_Push_OPND(Arithmetic, num);
				num = 0;
				while (Operate->element[Operate->top] != '#')//最后一个不是括号
				{
					num1 = Stack_Pop_OPND(Arithmetic);
					num2 = Stack_Pop_OPND(Arithmetic);
					if (Judge(Operate->element[Operate->top]) == 1)
						result = num2 + num1;
					else if (Judge(Operate->element[Operate->top]) == 2)
						result = num2 - num1;
					else if (Judge(Operate->element[Operate->top]) == 3)
						result = num2 * num1;
					else if (Judge(Operate->element[Operate->top]) == 4)
						result = num2 / num1;
					Stack_Pop_OPTR(Operate);
					Stack_Push_OPND(Arithmetic, result);
				}
				//printf("%d", Stack_Pop_OPND(Arithmetic));
				arr[i] = Stack_Pop_OPND(Arithmetic);
				i++;
				//break;
			}
			else
			{
				Stack_Pop_OPTR(Operate);//弹出此时的栈顶 )
				while (Operate->element[Operate->top] != '(')
				{
					num1 = Stack_Pop_OPND(Arithmetic);
					num2 = Stack_Pop_OPND(Arithmetic);
					if (Judge(Operate->element[Operate->top]) == 1)
						result = num2 + num1;
					else if (Judge(Operate->element[Operate->top]) == 2)
						result = num2 - num1;
					else if (Judge(Operate->element[Operate->top]) == 3)
						result = num2 * num1;
					else if (Judge(Operate->element[Operate->top]) == 4)
						result = num2 / num1;
					Stack_Pop_OPTR(Operate);
					Stack_Push_OPND(Arithmetic, result);
				}
				Stack_Pop_OPTR(Operate);//弹出此时的栈顶 (
				while (Operate->element[Operate->top] != '#')//处理玩括号后的运算
				{
					num1 = Stack_Pop_OPND(Arithmetic);
					num2 = Stack_Pop_OPND(Arithmetic);
					if (Judge(Operate->element[Operate->top]) == 1)
						result = num2 + num1;
					else if (Judge(Operate->element[Operate->top]) == 2)
						result = num2 - num1;
					else if (Judge(Operate->element[Operate->top]) == 3)
						result = num2 * num1;
					else if (Judge(Operate->element[Operate->top]) == 4)
						result = num2 / num1;
					Stack_Pop_OPTR(Operate);
					Stack_Push_OPND(Arithmetic, result);
				}
				
				arr[i] = Stack_Pop_OPND(Arithmetic);
				i++;
				
			}
		}
		temp = character;
	}
	return i;
}